﻿using Framework.MVC;
using Log.Model;
using Identity.Model;
using System;
using Account.Model;
using Identity.IService.Entry;
using Account.IService.Entry;
using Framework.MVVM.Model;
using Common.IService;
using Log.IService;
using Identity.IService;
using Account.IService;
using Account.Service;
using Identity.Service;
using Log.Service;
using Common.Service;
using Account.Model.Behavior;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IMobileService), typeof(MobileService), "Instance")]
    [ServiceAttribute(typeof(IAnswerService), typeof(AnswerService), "Instance")]
    [ServiceAttribute(typeof(IUserMailService), typeof(UserMailService), "Instance")]
    [ServiceAttribute(typeof(IUserService), typeof(UserService), "Instance")]
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class MyInfoModel : AsyncModelBase, IWriteLog, IValidateIdentity, IContainsUser, IGetUserMail, IContainsAnswers,IGetAccountByCode,IContainsMobile
    {
        #region Field

        private const string m_empty_identity = "myinfo000";
        private const string m_error_identity = "myinfo001";
        private const string m_complete_identity = "myinfo002";
        private const string m_error_user = "myinfo003";
        private const string m_complete_user = "myinfo004";
        private const string m_error_usermail = "myinfo005";
        private const string m_complete_usermail = "myinfo006";
        private const string m_error_answers = "myinfo007";
        private const string m_complete_answers = "myinfo008";
        private const string m_empty_account = "myinfo009";
        private const string m_error_account = "myinfo010";
        private const string m_complete_account = "myinfo011";

        private const string m_resource_user = "u000";
        private const char m_split = ';';

        private const string m_user = "user";
        private const string m_usermail = "usermail";
        private const string m_answers = "answers";
        private const string m_mobile = "mobile";
        private const string m_account="account";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string IdentityCode { get; set; }
        public string Source { get; set; }
        public bool IsOnce { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            AccountCode = entry.value;
            OnBeginRequest();
            this.ContainsUser(m_error_user, m_complete_user);
            OnBeginRequest();
            this.GetUserMail(m_error_usermail, m_complete_usermail);
            OnBeginRequest();
            this.ContainsMobile(m_user, string.Empty, string.Empty);
            OnBeginRequest();
            this.ContainsAnswers(m_user, m_error_answers, m_complete_answers);
            this.GetAccount(AccountResource, m_error_account, m_complete_account);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete("error", "identity");
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region IContainsUser

        public string AccountCode { get; set; }

        public void OnContainsUserComplete(string key, bool entry)
        {
            OnComplete(m_user, entry);
            this.Log(key, Source + m_split + AccountCode);
        }

        public void OnContainsUserError(string key)
        {
            this.Log(key, Source + m_split + AccountCode);
        }

        #endregion IContainsUser

        #region IGetUserMail

        public void OnGetUserMailComplete(string key, UserMailEntry entry)
        {
            OnComplete(m_usermail,entry);
            this.Log(key, Source + m_split + AccountCode);
        }

        public void OnGetUserMailError(string key)
        {
            this.Log(key, Source + m_split + AccountCode);
        }

        #endregion IGetUserMail

        #region IContainsAnswers

        public void OnContainsAnswersComplete(string key, bool entry)
        {
            OnComplete(m_answers,entry);
            this.Log(key, Source + m_split + AccountCode);
        }

        public void OnContainsAnswersError(string key)
        {
            this.Log(key, Source + m_split + AccountCode);
        }

        #endregion IContainsAnswers

        #region IContainsMobile

        public void OnContainsMobileComplete(string key, bool entry)
        {
            OnComplete(m_mobile, entry);
            OnEndRequest();
        }

        public void OnContainsMobileError(string key)
        {
            OnEndRequest();
        }

        #endregion IContainsMobile

        #region IGetAccountByCode

        public string AccountResource { get; set; }

        public void OnGetAccountByCodeComplete(string key, string name)
        {
            OnComplete(m_account, name);
            this.Log(key, name);
        }

        public void OnGetAccountByCodeError(string key)
        {
            this.Log(key, Source + m_split + AccountCode);
        }

        #endregion IGetAccountByCode
    }
}